package com.xuecheng.media.domain.event.handler;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.media.domain.event.MediaUploadRecordEvent;
import com.xuecheng.media.domain.model.entity.UploadRecord;
import com.xuecheng.media.domain.service.UploadRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-08-18  10:41
 */
@Component
public class MediaUploadRecordEventHandler {

    @Autowired
    private UploadRecordService uploadRecordService;

    @Async
    @EventListener(classes = MediaUploadRecordEventHandler.class)
    public void uploadListener(MediaUploadRecordEvent event){
        // 查询是否存在记录
        UploadRecord one = uploadRecordService.getOne(new LambdaQueryWrapper<UploadRecord>()
                .eq(UploadRecord::getFileId, event.getFileMd5()));
        if (Objects.isNull(one)){
            uploadRecordService.save(UploadRecord.getDefault(event.getFileMd5()));
        } else {
            one.setStatus(event.getStatus());
            uploadRecordService.updateById(one);
        }

    }
}
